<template>
  <div
    ref="dialog"
    v-if="visible"
    class="rewrite-result-toolbar"
    :style="toolbarStyle"
    @click.stop="handleToolbarClick"
    @mousedown.stop="handleToolbarMouseDown"
  >
    <div class="rewrite-result-content">
      <div class="rewrite-header">
        <span class="rewrite-title">{{ dialogTitle }}</span>
        <button class="rewrite-close" @click.stop="handleClose">×</button>
      </div>
      <div class="text-sections">
        <div class="text-section">
          <div class="section-label">{{ originalLabel }}：</div>
          <div class="text-content original">{{ originalText }}</div>
        </div>
        <div class="text-section">
          <div class="section-label">{{ resultLabel }}：</div>
          <div class="text-content rewritten" v-html="formatRewriteResult(resultText)"></div>
        </div>
      </div>
      <div class="rewrite-actions">
        <button class="action-btn cancel-btn" @click.stop="handleCancel">
          取消
        </button>
        <button class="action-btn apply-btn" @click.stop="handleApply">
          {{ applyButtonText }}
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'RewriteResultDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    originalText: {
      type: String,
      default: ''
    },
    resultText: {
      type: String,
      default: ''
    },
    position: {
      type: Object,
      default: () => ({ x: 0, y: 0 })
    },
    // 新增功能类型属性
    functionType: {
      type: String,
      default: 'rewrite', // 'rewrite', 'continue' 或 'abbreviate' 或 'translate'
      validator: value => ['rewrite', 'continue', 'abbreviate', 'translate','smartCorrection'].includes(value)
    }
  },
  data() {
    return {
      adjustedTop: null
    }
  },
  computed: {
    toolbarStyle() {
      const top = this.adjustedTop !== null ? this.adjustedTop : this.position.y
      return {
        left: `${this.position.x}px`,
        top: `${top}px`
      }
    },
    // 根据功能类型动态计算对话框标题
    dialogTitle() {
      switch (this.functionType) {
        case 'continue':
          return '续写结果'
        case 'abbreviate':
          return '缩写结果'
        case 'translate':
          return '翻译结果'
        case 'smartCorrection':
          return '智能纠错结果'
        default:
          return '改写结果'
      }
    },
    // 根据功能类型动态计算原文标签
    originalLabel() {
      return '原文'
    },
    // 根据功能类型动态计算结果标签
    resultLabel() {
      switch (this.functionType) {
        case 'continue':
          return '续写结果'
        case 'abbreviate':
          return '缩写结果'
        case 'translate':
          return '翻译结果'
        case 'smartCorrection':
          return '纠错结果'
        default:
          return '改写结果'
      }
    },
    // 根据功能类型动态计算应用按钮文本
    applyButtonText() {
      switch (this.functionType) {
        case 'continue':
          return '应用续写'
        case 'abbreviate':
          return '应用缩写'
        case 'translate':
          return '应用翻译'
        case 'smartCorrection':
          return '应用纠错'
        default:
          return '应用改写'
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$nextTick(this.adjustPosition)
        window.addEventListener('resize', this.adjustPosition)
      } else {
        this.adjustedTop = null
        window.removeEventListener('resize', this.adjustPosition)
      }
    }
  },
  methods: {
    // 工具栏点击事件
    handleToolbarClick(event) {
      event.preventDefault()
    },

    // 工具栏鼠标按下事件
    handleToolbarMouseDown(event) {
      event.preventDefault()
    },

    // 格式化改写结果用于显示
    formatRewriteResult(text) {
      if (!text) return ''
      
      // 删除<think>\n\n</think>\n\n标记
      let cleanedText = text.replace(/<think>\s*\n\s*\n\s*<\/think>\s*\n\s*\n/g, '')
      
      // 转义HTML特殊字符
      const escapedText = cleanedText
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#39;')
      
      // 将换行符转换为<br>标签
      const htmlWithBreaks = escapedText.replace(/\n/g, '<br>')
      
      return htmlWithBreaks
    },

    // 关闭弹窗
    handleClose() {
      this.$emit('close')
    },

    // 取消操作
    handleCancel() {
      this.$emit('cancel')
      // 发送返回到工具栏的事件
      this.$emit('back-to-toolbar')
    },

    // 应用改写或续写
    handleApply() {
      this.$emit('apply', {
        originalText: this.originalText,
        resultText: this.resultText,
        functionType: this.functionType
      })
    },

    adjustPosition() {
      this.$nextTick(() => {
        const dialog = this.$refs.dialog
        if (dialog) {
          const rect = dialog.getBoundingClientRect()
          const windowHeight = window.innerHeight
          let top = this.position.y
          if (rect.top + rect.height > windowHeight) {
            // 超出底部，调整 top
            const overflow = rect.top + rect.height - windowHeight
            top = Math.max(0, this.position.y - overflow)
          }
          this.adjustedTop = top
        }
      })
    }
  }
}
</script>

<style scoped>
/* 改写结果工具栏样式 */
.rewrite-result-toolbar {
  position: fixed;
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 99999;
  font-size: 12px;
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.95);
  pointer-events: auto;
  min-width: 400px;
  max-width: 600px;
}

.rewrite-result-content {
  padding: 12px;
}

.rewrite-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid #e5e7eb;
}

.rewrite-title {
  font-weight: 500;
  color: #2c2c36;
  font-size: 14px;
}

.rewrite-close {
  background: none;
  border: none;
  color: #999;
  font-size: 16px;
  cursor: pointer;
  padding: 0;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 2px;
}

.rewrite-close:hover {
  background: #f0f0f0;
  color: #666;
}

.text-sections {
  margin-bottom: 12px;
}

.text-section {
  margin-bottom: 12px;
}

.section-label {
  font-weight: 500;
  color: #2c2c36;
  font-size: 12px;
  margin-bottom: 6px;
}

.text-content {
  padding: 8px;
  border-radius: 4px;
  font-size: 12px;
  line-height: 1.4;
  min-height: 40px;
  word-wrap: break-word;
  border: 1px solid;
}

.text-content.original {
  background-color: #f8f9fa;
  border-color: #e9ecef;
  color: #495057;
}

.text-content.rewritten {
  background-color: #e8f5e8;
  border-color: #c3e6c3;
  color: #2d5a2d;
}

.rewrite-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.action-btn {
  padding: 6px 12px;
  border: 1px solid;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 60px;
}

.action-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.cancel-btn {
  background-color: #fff;
  border-color: #e8eaf2;
  color: #2c2c36;
}

.cancel-btn:hover {
  color: #196ef7;
  border-color: #196ef7;
}

.apply-btn {
  background-color: #196ef7;
  border-color: #196ef7;
  color: #fff;
}

.apply-btn:hover {
  background-color: #4288f8;
  border-color: #4288f8;
  color: #fff;
}
</style> 